<?php
namespace Home\Controller;
use Think\Controller;
use Think\Image;
class FinancialController extends PublicController {


	function _initialize() {
		parent::_initialize ();
	}
	//代理商入账消费
   
    //用户入账消费
    public function customer(){
        $user_id = $_SESSION ["userInfo"]['user_id'];
        $type = I('type');
        if(empty($type)){
            $type = 1;
        }
        if(!$user_id){
            $this->error('请填写完整信息');
        }
		$search = I('search');
        if($search){
            $where['a.user_name'] = array('like',"%$search%");
            $this->assign("search", $search);
        }

        if(I('p')){
            $p = I('p');
        }else{
             $p = 1;
        }
        $where['a.user_id'] = $user_id;
        $where['c.type'] = $type;
        $header = C('header')['customer'];
        $count =  M('ucharge')
                    ->alias("c")
                    ->join("left join pt_user as a on a.user_id=c.uid")
                    ->field('c.*,a.user_name')
                    ->where($where)
                    ->count();
        
        $Page = new \Think\Page($count, 30);
        $Page->setConfig('theme', "%UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show = $Page->show(); // 分页显示输出

        $list = M('ucharge')
                ->alias("c")
                ->join("left join pt_user as a on a.user_id=c.uid")
                ->field('c.charge_id,a.user_name,c.amount,c.logtime')
                ->where($where)
                ->order('charge_id desc')
                ->limit($Page->firstRow . ',' . $Page->listRows)
                ->select();
        foreach ($list as $k => $v) {
            $list[$k]['logtime'] = date('Y-m-d H:i:s',$v['logtime']);
        }
        // var_dump($show);exit;
        $show = str_replace('<div>','',$show);
        $show = str_replace('current','active',$show);
        $show = str_replace('<<<','上一页<',$show);
        $show = str_replace('>>>','>下一页',$show);
        $show = str_replace('</div>','',$show);
        // echo  htmlentities($show,ENT_QUOTES,"UTF-8");exit;
        // $show = preg_replace('//s*/', '', $show);
        if($count <= 10){
            $show = '<a href="" class="active">1</a>';
        }
        $this->assign("page", $show); // 赋值分页输出
        $this->assign("count", $count);
        $this->assign("p", $p);
        $this->assign("header", $header);
        $this->assign("list", $list);
        $this->display();
    }

    



    public function charge(){
        if(IS_POST){
            $request = I('post.');
            // var_dump($request);exit;
            if(empty($request['phone']) || empty($request['amount'])){
                $this->error('请填写完整信息');
            }
            $where['phone'] = $request['phone'];
            $where['admin_id'] = $_SESSION["userInfo"]['admin_id'];
            $list = M('user')->where($where)->find();
            if(empty($list)){
                $this->error('手机号用户不存在');
            }

            $res['uid'] =  $list['user_id'];
            $res['admin_id'] =  $_SESSION["userInfo"]['admin_id'];
            $res['amount'] =  $request['amount'];
            $res['logtime'] =  time();
            $res['type'] = 1;

            $data = M('ucharge')->add($res);
            if(empty($data)){
                $this->error('充值失败');
            }else{
                $this->success("充值成功", U("Financial/charge"));exit;
            }
        }
        $this->display();
    }




    
}